<template>
  <div class="assignment-submit">
    <h1>提交答案:</h1>
    <form @submit.prevent="submitAnswer" class="submit-form">
      <div class="form-group">
        <label for="submissionContent">提交内容：</label>
        <textarea
            v-model="submissionContent"
            id="submissionContent"
            required
            placeholder="请输入试题答案..."
            class="textarea"
        ></textarea>
      </div>
      <button type="submit" class="submit-button">提交答案</button>
      <p v-if="successMessage" class="success">{{ successMessage }}</p>
      <p v-if="errorMessage" class="error">{{ errorMessage }}</p>
    </form>
  </div>
</template>

<script>
import axios from 'axios';
import { BASE_URL } from '@/config';

export default {
  data() {
    return {
      submissionContent: '',
      successMessage: '',
      errorMessage: '',
    };
  },
  methods: {
    async submitAnswer() {
      if (!this.submissionContent.trim()) {
        this.errorMessage = '提交内容不能为空。';
        return;
      }
      try {
        const response = await axios.post(`${BASE_URL}/exam/UpdateStandard`, {
          exam_id:this.$route.query.exam_id,
          standard: this.submissionContent,
        });
        if (response.data.code === 200 || response.data.code === "200") {
          this.successMessage = response.data.message;
          this.submissionContent = '';
        }
      } catch (error) {
        this.errorMessage = '提交答案失败，请重试。';
        console.error('提交失败:', error);
      }
    },
  },
};
</script>

<style scoped>
.assignment-submit {
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  max-width: 600px;
  margin: 0 auto;
}

h1 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.submit-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

label {
  font-weight: bold;
  color: #555;
}

.textarea {
  width: 100%;
  height: 100px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 10px;
  font-size: 14px;
  resize: none;
  transition: border-color 0.3s;
}

.textarea:focus {
  border-color: #3f51b5;
  outline: none;
}

.submit-button {
  background-color: #3f51b5;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.submit-button:hover {
  background-color: #2c387e;
}

.success {
  color: green;
  text-align: center;
}

.error {
  color: red;
  text-align: center;
}

</style>
